import { createApp } from "vue";
import InputFileDialog from "../components/dialog/InputFileDialog.vue";

export default {
  install: (app, options) => {
    // Dialog插件,option为配置选项

    const inputDialog = (title,fileName) => {
      //封装成Promise
      return new Promise((resolve) => {
        const mountNode = document.createElement("div");
        document.body.appendChild(mountNode);

        const confirm = function (rename) {
          //触发回调
          console.log("触发回调confirm rename:", rename);
          dialogApp.unmount();
          document.body.removeChild(mountNode);
          resolve(rename);
        };

        const close = function () {
          console.log("触发关闭回调，卸载组件close");
          //触发关闭回调，卸载组件
          dialogApp.unmount();
          document.body.removeChild(mountNode);
          //返回空
          resolve('');
        };

        //挂载到div节点上进行显示
        const dialogApp = createApp(InputFileDialog, {
          title,
          close,
          confirm,
          fileName
        });
        dialogApp.mount(mountNode);
      });
    };
    app.provide("inputDialog", inputDialog);
  },
};
